import sys
sys.path.insert(0,'../')
from RestAPI.core.APIExceptions import *
from RestAPI.core.APIwrapper import *
import unittest

class testRestRequest(unittest.TestCase):
    def setUp(self):
        return
    def testRestRequest(self):
        print "Test generate request\n======================================================"
        req=RestRequest('www.python.org')
        print req.__class__
#        self.assertEqual(req.__class__.__repr__,"RestAPI.core.APIwrapper.RestRequest")
    def testGET(self):
        print "Test GET method\n======================================================"
        req=RestRequest('http://scholar.google.com/').GET('/scholar').\
                                addSubResources({'hl':'en','q':'test m'}).\
                                addHeader('User-Agent','Mozilla/4.0')
        print "Final url for get is : %s" % req.getUrl()
        if req.send():
            print "Result of the above request is:\n============================================="
            print req.result
            print "Its headers are:\n============================================="
            print req.response.getheaders()
            print "Its content type is:\n============================================="
            print req.response.getheader('Content-type')
        else:
            raise Exception("HTTP error %s : %s!" % (req.response.status,req.response.reason))
#        req.succed
    def testSetHost(self):
        print "Test setHost method\n======================================================"
        req=RestRequest().setHost('http://www.scholar.google.com/')
        print req.host
        self.assertEqual(req.host,'www.scholar.google.com')
    def testPOST(self):
        print "Test POST method\n======================================================"
        req=RestRequest('http://cgi-lib.berkeley.edu/').POST('/ex/fup.cgi').form({'upfile':'testS3.py','note':'a test'})
        print req.headers
        print req.body
        if req.send():
            print "Result of the above request is:\n============================================="
            print req.result
        else:
            raise Exception("HTTP error %s : %s!" % (req.response.status,req.response.reason))
    def testPUT(self):
        print "Test PUT method\n======================================================"
        req=RestRequest('scholar.google.com').PUT('/').file('testS3.py')
        print req.headers
        print req.body
if __name__ == '__main__':
    unittest.main()
